//
//  YeeCouponPopView.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/8.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeCouponPopView.h"
@interface YeeCouponTableViewCell : UITableViewCell

@property(nonatomic,retain)UILabel  *titleLable;
@property(nonatomic,retain)UILabel  *priceLable;
@property(nonatomic,retain)UILabel  *dateLable;
@property(nonatomic,retain)UIButton *button;




@end


@implementation YeeCouponTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
      
        [self addOwnView];
    }
    return self;
}
-(void)addOwnView
{
    
    _button=[UIButton buttonWithType:UIButtonTypeCustom];
    [_button setTitle:@"领取" forState:UIControlStateNormal];
    _button.titleLabel.font=[UIFont Y_systemFontOfSize:13];
    _button.layer.cornerRadius=5.0f;
    _button.layer.masksToBounds=YES;
    _button.layer.borderWidth=0.3f;
     [_button setTitleColor:kNavBarThemeColor forState:UIControlStateNormal];
    _button.layer.borderColor=kNavBarThemeColor.CGColor;
    [self.contentView addSubview:_button];
    
    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).with.offset(-10);
        make.height.mas_equalTo(27);
        make.width.mas_equalTo(45);
    }];
    
    _titleLable=[[UILabel alloc] init];
    _titleLable.textColor=kBlackColor;
    _titleLable.font=[UIFont Y_systemFontOfSize:12];
    _titleLable.text=@"订单满2500元使用(不含邮费)";
    [self.contentView addSubview:_titleLable];
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.left.equalTo(self.contentView).with.offset(15);
         make.height.mas_equalTo(21);
         make.centerY.equalTo(self.contentView);
    }];
    
    _priceLable=[[UILabel alloc] init];
    _priceLable.textColor=kNavBarThemeColor;
    _priceLable.font=[UIFont Y_systemFontOfSize:16];
    _priceLable.text=@"￥80";
    [self.contentView addSubview:_priceLable];
    [_priceLable mas_makeConstraints:^(MASConstraintMaker *make)
     {   make.left.equalTo(self.contentView).with.offset(15);
         make.height.mas_equalTo(21);
         make.bottom.equalTo(_titleLable.mas_top).offset(5);
     }];
    
    _dateLable=[[UILabel alloc] init];
    _dateLable.textColor=kGrayColor;
    _dateLable.font=[UIFont Y_systemFontOfSize:11];
    _dateLable.text=@"2017-05-30";
    [self.contentView addSubview:_dateLable];
    [_dateLable mas_makeConstraints:^(MASConstraintMaker *make)
     {   make.left.equalTo(self.contentView).with.offset(15);
         make.height.mas_equalTo(21);
         make.top.equalTo(_titleLable.mas_bottom).with.offset(-5);
     }];
    
}
@end
@interface YeeCouponPopView ()<UITableViewDelegate,UITableViewDataSource>
{

    UILabel       *_titleLable;
    UIButton      *_closeButton;
    UITableView   *_tableView;
    UIView        *_contentView;
    NSString      *_titleString;
    NSString      *_cancelString;


}
@end
@implementation YeeCouponPopView
-(instancetype)initWithFrame:(CGRect)frame
{
 
    if (self=[super initWithFrame:frame])
    {
        [self setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
        UITapGestureRecognizer  *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickcover)];
        [self addGestureRecognizer:tap];
        
        [self addOwnView];
        
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title cancelTitle:(NSString*)cancelTitle
{
    if (self=[super initWithFrame:frame])
    {
        [self setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
        UITapGestureRecognizer  *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickcover)];
        [self addGestureRecognizer:tap];
        _titleString=title;
        _cancelString=cancelTitle;
        [self addOwnView];
        
    }
    return self;
}
-(void)addOwnView
{
    _contentView=[[UIView alloc] init];
    [_contentView setBackgroundColor:kWhiteColor];
    [_contentView setFrame:CGRectMake(0, self.height-(kMainScreenWidth+70), kMainScreenWidth, kMainScreenWidth+70)];
    [self addSubview:_contentView];
    
    _titleLable=[[UILabel alloc] init];
    _titleLable.textColor=kBlackColor;
    _titleLable.font=[UIFont Y_systemFontOfSize:14];
    _titleLable.text= _titleString ?_titleString:@"优惠券";
    _titleLable.textAlignment=NSTextAlignmentCenter;
    [_titleLable setFrame:CGRectMake(0, 0, kMainScreenWidth, 30)];
    [_contentView addSubview:_titleLable];
    
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 30, kMainScreenWidth, kMainScreenWidth)];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    [_contentView addSubview:_tableView];
    
    _closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_closeButton setTitle: _cancelString?_cancelString:@"关闭" forState:UIControlStateNormal];
    [_closeButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [_closeButton setBackgroundColor:kNavBarThemeColor];
    _closeButton.titleLabel.font=[UIFont Y_systemFontOfSize:14];
    [_closeButton setFrame:CGRectMake(0, kMainScreenWidth+30, kMainScreenWidth, 40)];
    __weak typeof(self)weakelf=self;
    [[_closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x)
     {
         [weakelf removeFromSuperview];
         
     }];
    [_contentView addSubview:_closeButton];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YeeCouponTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YeeCouponTableViewCell class])];
    if (cell==nil)
    {
        cell=[[YeeCouponTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([YeeCouponTableViewCell class])];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
//cell顶格
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
-(void)clickcover
{
    [self removeFromSuperview];
    
}

@end
